package com.qf.servlet;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/13 20:07
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/dashboard")
public class AdminController {

    @Autowired
    DtsUserService dtsUserService;//用户

    @Autowired
    DtsGoodsService dtsGoodsService;//商品

    @Autowired
    DtsGoodsProductService dtsGoodsProductService;//货品

    @Autowired
    DtsOrderService dtsOrderService;//订单

    @Autowired
    StatService statService;
    /**
     * 统计用户数, 商品数, 库存货品数,订单数
     */

    @RequestMapping("")
    public Object count(){

        long count = dtsGoodsProductService.getCount();
        long count1 = dtsGoodsService.getCount();
        long count2 = dtsOrderService.getCount();
        long count3 = dtsUserService.getCount();

        Map<String,Object> data = new HashMap<>();
        data.put("goodsTotal",count1);
        data.put("userTotal",count3);
        data.put("productTotal",count);
        data.put("orderTotal",count2);

        return ResponseUtil.ok(data);
    }

    /**
     * 可视化图形，饼图，柱状图，曲线图
     */
    @RequestMapping("/chart")
    public Object visualization(){
        /**
         * 饼图  商品分类和对应分类的销售额
         */
        //获取统计
        List<CategorySellAmts> sellAmts = statService.findCategorySell();
//        创建数组保存名字
        String [] name = new String [sellAmts.size()];

        for(int i=0 ; i<sellAmts.size() ; i++){
//            遍历且赋值给数组
            name[i] = sellAmts.get(i).getName();
        }

        //封装饼图结果
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(name);
        categorySellVo.setCategorySellData(sellAmts);


        /**
         * 柱状图  日期 和对应的 订单总额,  订单笔数
         */

//        获取所有订单
        List<DayStatis> order = statService.findOrder();

        //提取日期
        List<String> orderData = extractData(order);

        //提取订单总额
        List<BigDecimal> orderTotal = extractTotal(order,orderData);
        //提取订单交易数
        List<Integer> orderQuantity = extractQuantity(order,orderData);

        //封装柱状图结果
        Map<String,Object> map = new HashMap<>();
        map.put("dayData", orderData);
        map.put("orderAmtData", orderTotal);
        map.put("orderCntData", orderQuantity);

        /**
         * 曲线图
         */
//       获取所有用户
        List<DayStatis> user = statService.findUser();

        //提取日期
        List<String> dayData1 = extractData(user);
        //两个日期合并
        dayData1 = mergeDate(orderData,dayData1);
        //提取用户的数量
        List<Integer>userTotal = extractUserTotal(user,dayData1);
        //提取订单的数量
//        List<Integer> orderQunantity = extractQuantity(user, dayData1);

        //封装曲线图结果
        Map<String,Object> mapUser = new HashMap<>();
        mapUser.put("dayData",dayData1);
        mapUser.put("userCnt",userTotal);
        mapUser.put("orderCnt",orderQuantity);

        //封装结果
        Map<String,Object> data = new HashMap<>();
        //饼图
        data.put("categorySell",categorySellVo);
        //柱状图
         data.put("orderAmts",map);
        //曲线图
        data.put("userOrderCnt",mapUser);

        return ResponseUtil.ok(data);
    }

    //提取用户数
    private List<Integer> extractUserTotal(List<DayStatis> user, List<String> dayData1) {

        List<Integer> list = new ArrayList<>();
//        循环日期
        for (String s : dayData1) {
            for (DayStatis dayStatis : user) {
                if(s.equalsIgnoreCase(dayStatis.getDayStr())){
                    list.add(dayStatis.getCnts());
                }
            }
        }
        return list;
    }

    //合并日期
    private List<String> mergeDate(List<String> orderData, List<String> dayData1) {

        //使用set集合自动去重的功能
        Set<String> set = new HashSet<>();
        set.addAll(orderData);
        set.addAll(dayData1);
        //去重成功
        List<String> list = new ArrayList<>();
        list.addAll(set);

        //排序
        Collections.sort(list);

        return list;
    }

    //提取订单交易数
    private List<Integer> extractQuantity(List<DayStatis> order, List<String> orderData) {


        //循环订单表和日期表，将对应的订单交易数存入集合
        List<Integer> list = new ArrayList<>();

        for (String orderDatum : orderData) {
            for (DayStatis dayStatis : order) {
                if(orderDatum.equalsIgnoreCase(dayStatis.getDayStr())){
                    list.add(dayStatis.getCnts());
                }
            }
        }
        return list;
    }

    //提取订单总额
    private List<BigDecimal> extractTotal(List<DayStatis> order, List<String> orderData) {
//        对订单表结果，日期进行循环，将对应的日期和订单总额匹配

        List<BigDecimal> list = new ArrayList<>();

        for (String orderDatum : orderData) {
            for (DayStatis dayStatis : order) {
                if(orderDatum.equalsIgnoreCase(dayStatis.getDayStr())){
                    list.add(dayStatis.getAmts());
                }
            }
        }
        return list;
    }
    //提取日期的方法
    private List<String> extractData(List<DayStatis> order) {

        //提取日期
        List<String> collect = order.stream().map(dayStatis -> {
            return dayStatis.getDayStr();
        }).collect(Collectors.toList());
        //对日期进行排序
        Collections.sort(collect);

        return collect;

    }
}
